﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;

namespace CSF.Utility.Security
{
    public static class UserSessionCodec
    {
        /// <summary>
        /// 生成SessionId
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="ipAddress"></param>
        /// <param name="loginTime"></param>
        /// <returns></returns>
        public static string GenerateSessionId(string userName, string password,
            string ipAddress, DateTime loginTime)
        {
            string s = userName + password + ipAddress + loginTime.Ticks.ToString();
            var hasher = MD5.Create();
            byte[] sbytes = Encoding.UTF8.GetBytes(s);
            byte[] hashvalue = hasher.ComputeHash(sbytes);

            string result = HttpServerUtility.UrlTokenEncode(hashvalue);
            return result;
        }
    }
}
